//
//  GQHomeDetailController.m
//  GuQu
//
//  Created by HELIUM STAR on 15/5/12.
//  Copyright (c) 2015年 HELIUM STAR. All rights reserved.
//

#import "GQHomeDetailController.h"
#import "GQHomeKeyBoardToolBar.h"
#import "GQHomeDetailViewCell.h"

@interface GQHomeDetailController () <UITextFieldDelegate, UITableViewDataSource, UITableViewDelegate>

@property (strong, nonatomic) GQHomeKeyBoardToolBar *keyBoardToolBar;

@end

@implementation GQHomeDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageWithOriginalName:@"distribute_back"] style:UIBarButtonItemStyleDone target:self action:@selector(backToPre)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageWithOriginalName:@"discover_more"] style:UIBarButtonItemStyleDone target:self action:@selector(shareItemDidClick)];
    
    self.navigationItem.title = @"详情";
    self.view.backgroundColor = [UIColor lightGrayColor];
    GQHomeKeyBoardToolBar *toolBar = [[GQHomeKeyBoardToolBar alloc] initWithFrame:CGRectMake(0, self.view.height - kToolBarHeight, self.view.width, kToolBarHeight)];
    toolBar.backgroundColor = [UIColor whiteColor];
    toolBar.layer.borderColor = [UIColor lightGrayColor].CGColor;
    toolBar.layer.borderWidth = 1;
    
    self.keyBoardToolBar = toolBar;
    toolBar.textField.returnKeyType = UIReturnKeySend;
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureDidClick)];
    [self.view addGestureRecognizer:tapGesture];
    toolBar.textField.delegate = self;
    
    //创建UITableView
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height - kToolBarHeight) style:UITableViewStylePlain];
    [self.view addSubview:tableView];
    tableView.delegate = self;
    tableView.dataSource = self;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    [self.view addSubview:toolBar];
    
}

- (void)shareItemDidClick
{
    [self setUpShareItem];
}

- (void)setUpShareItem
{
    UIView *coverView = [[UIView alloc] initWithFrame:self.view.frame];
    [self.view addSubview:coverView];
    coverView.backgroundColor = [UIColor blackColor];
    coverView.alpha = 0.5;
    
    CGFloat shareHeight = (240 / 568.0) * self.view.height;
    UIView *shareView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.height - shareHeight, self.view.width, shareHeight)];
    shareView.backgroundColor = [UIColor whiteColor];
    CGFloat marginX = 15;
    CGFloat marginY = 20;
    CGFloat margin = 25;
    CGFloat width = (self.view.width - 2 * marginX - 3 * margin) / 4;
    NSInteger row = 4;
    for (int i = 0; i < 8; i++) {
        UIView *containView = [[UIView alloc] init];
        containView.x = marginX + (width + margin) * (i % row);
        containView.width = width;
        containView.height = width + 20;
        containView.y = marginY + (containView.height + 18) * (i / row);
        containView.backgroundColor = [UIColor orangeColor];
        [shareView addSubview:containView];
    }
    [self.view addSubview:shareView];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *flag = @"flag";
    GQHomeDetailViewCell *cell = [tableView dequeueReusableCellWithIdentifier:flag];
    if (cell == nil) {
        cell = [[GQHomeDetailViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:flag];
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 700;
}


- (void)tapGestureDidClick
{
    if (self.keyBoardToolBar.y < self.view.height - self.keyBoardToolBar.height) {
        self.keyBoardToolBar.textField.text = @"";
        [self.view endEditing:YES];
    }
    
}

- (void)keyBoardWillShow:(NSNotification *)noti
{
    CGRect keyBoardFrame = [[noti.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat duration = [[noti.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    
    [UIView animateWithDuration:duration animations:^{
        self.keyBoardToolBar.transform = CGAffineTransformMakeTranslation(0, -keyBoardFrame.size.height);
    } completion:^(BOOL finished) {
        
    }];
    
}

- (void)keyBoardWillHide:(NSNotification *)noti
{
    [UIView animateWithDuration:0.25 animations:^{
        self.keyBoardToolBar.transform = CGAffineTransformIdentity;
    } completion:^(BOOL finished) {
        
    }];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    textField.text = @"";
    [self.view endEditing:YES];
    return YES;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewWillAppear:(BOOL)animated
{
    [self.tabBarController.tabBar removeFromSuperview];
}

- (void)backToPre
{
    [self.navigationController popViewControllerAnimated:YES];
}



@end
